package cn.hahah.module.blog.controller.admin.userfriend;

import cn.hahah.module.blog.dal.dataobject.users.UsersDO;
import cn.hahah.module.blog.service.users.UsersService;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.hahah.framework.common.pojo.PageParam;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.common.pojo.CommonResult;
import cn.hahah.framework.common.util.object.BeanUtils;
import static cn.hahah.framework.common.pojo.CommonResult.success;

import cn.hahah.framework.excel.core.util.ExcelUtils;

import cn.hahah.framework.apilog.core.annotation.ApiAccessLog;
import static cn.hahah.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.hahah.module.blog.controller.admin.userfriend.vo.*;
import cn.hahah.module.blog.dal.dataobject.userfriend.UserFriendDO;
import cn.hahah.module.blog.service.userfriend.UserFriendService;

@Tag(name = "管理后台 - 用户好友")
@RestController
@RequestMapping("/blog/user-friend")
@Validated
public class UserFriendController {

    @Resource
    private UserFriendService userFriendService;
    @Resource
    private UsersService usersService;

    @PostMapping("/create")
    @Operation(summary = "创建用户好友")
    @PreAuthorize("@ss.hasPermission('blog:user-friend:create')")
    public CommonResult<Long> createUserFriend(@Valid @RequestBody UserFriendSaveReqVO createReqVO) {
        return success(userFriendService.createUserFriend(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户好友")
    @PreAuthorize("@ss.hasPermission('blog:user-friend:update')")
    public CommonResult<Boolean> updateUserFriend(@Valid @RequestBody UserFriendSaveReqVO updateReqVO) {
        userFriendService.updateUserFriend(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户好友")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:user-friend:delete')")
    public CommonResult<Boolean> deleteUserFriend(@RequestParam("id") Long id) {
        userFriendService.deleteUserFriend(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户好友")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:user-friend:query')")
    public CommonResult<UserFriendRespVO> getUserFriend(@RequestParam("id") Long id) {
        UserFriendDO userFriend = userFriendService.getUserFriend(id);
        return success(BeanUtils.toBean(userFriend, UserFriendRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户好友分页")
    @PreAuthorize("@ss.hasPermission('blog:user-friend:query')")
    public CommonResult<PageResult<UserFriendRespVO>> getUserFriendPage(@Valid UserFriendPageReqVO pageReqVO) {
        PageResult<UserFriendDO> pageResult = userFriendService.getUserFriendPage(pageReqVO);
        PageResult<UserFriendRespVO> result = BeanUtils.toBean(pageResult, UserFriendRespVO.class);
        for (UserFriendRespVO userFriendRespVO : result.getList()) {
            UsersDO usersDO = usersService.getUsers(userFriendRespVO.getUserId());
            UsersDO friendDo = usersService.getUsers(userFriendRespVO.getFriendId());

            if(usersDO != null){
                userFriendRespVO.setFriendName(usersDO.getUsername());
            }
            if(friendDo != null){
                userFriendRespVO.setUserName(friendDo.getUsername());
            }
        }
        return success(result);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户好友 Excel")
    @PreAuthorize("@ss.hasPermission('blog:user-friend:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserFriendExcel(@Valid UserFriendPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserFriendDO> list = userFriendService.getUserFriendPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户好友.xls", "数据", UserFriendRespVO.class,
                        BeanUtils.toBean(list, UserFriendRespVO.class));
    }

}